﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Controler
{
    using W = WindowsServiceHelper;
    public partial class Form1 : Form
    {
        XmlHelper xmlHelper;

        public Form1()
        {
            InitializeComponent();
            xmlHelper = new XmlHelper();
        }

        string serviceFilePath = $"{Application.StartupPath}\\BingDesktop.exe";
        string serviceName = "AGrabBingImg";

        private void ValidateServiceStatus()
        {
            if (WindowsServiceHelper.IsServiceExisted(serviceName))
            {
                lbServiceStallStatus.Text = "服务已安装";
                lbServiceStallStatus.ForeColor = Color.Green;
            }
            else
            {
                lbServiceStallStatus.Text = "服务未安装";
                lbServiceStallStatus.ForeColor = Color.Red;
            }

            if (WindowsServiceHelper.IsServiceRun("AGrabBingImg"))
            {
                lbServiceStatus.Text = "服务已启动";
                lbServiceStatus.ForeColor = Color.Green;
            }
            else
            {
                lbServiceStatus.Text = "服务未启动";
                lbServiceStatus.ForeColor = Color.Red;
            }
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            if (W.IsServiceExisted(serviceName))
            {
                MessageBox.Show("服务已经存在");
            }
            else
                W.InstallService(serviceFilePath);
            ValidateServiceStatus();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (W.IsServiceExisted(serviceName))
            {
                W.ServiceStart(serviceName);

                lbServiceStatus.Text = "服务已启动";
                lbServiceStatus.ForeColor = Color.Green;
            }
            else
                MessageBox.Show("服务不存在");
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (W.IsServiceExisted(serviceName))
                W.ServiceStop(serviceName);
            else
                MessageBox.Show("服务不存在");
            ValidateServiceStatus();
        }

        private void btnUninstall_Click(object sender, EventArgs e)
        {
            if (W.IsServiceExisted(serviceName))
            {
                W.ServiceStop(serviceName);
                W.UninstallService(serviceFilePath);
            }
            else
                MessageBox.Show("服务不存在");
            ValidateServiceStatus();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lbImgFolder.Text = xmlHelper.Read(Setting.SaveImgPath);
            txtBingUrl.Text = xmlHelper.Read(Setting.BingUrl);
            txtRegex.Text = xmlHelper.Read(Setting.ImgRegex);
            txtImgType.Text = xmlHelper.Read(Setting.ImgType);
            txtServiceCron.Text = xmlHelper.Read(Setting.ServiceCron);
            ValidateServiceStatus();
            ShowPic();
        }

        private void btnOPenFolder_Click(object sender, EventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var path = $@"{dialog.SelectedPath}\";
                lbImgFolder.Text = path;
                xmlHelper.Write(Setting.SaveImgPath, path);
            }
        }

        #region {图片预览}

        public int ImgCurrentIndex = 0;

        private bool ShowPic()
        {
            var path = xmlHelper.Read(Setting.SaveImgPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                return false;
            }

            if (ImgCurrentIndex < 0)
                return false;
            var imgs = Directory.GetFiles(path, $"*{xmlHelper.Read(Setting.ImgType)}*");
            var pics = new PictureBox[] { pic5, pic4, pic3, pic2, pic1 };
            var labs = new Label[] { lab5, lab4, lab3, lab2, lab1 };

            if (ImgCurrentIndex + 1 > imgs.Length)
                return false;

            new ImgPreview { CurrentIndex = ImgCurrentIndex }.ToShow(imgs, pics, labs);
            picEnlarge.ImageLocation = pic5.ImageLocation;
            return true;
        }

        private void btnImgPre_Click(object sender, EventArgs e)
        {
            ImgCurrentIndex += 1;
            if (!ShowPic())
                ImgCurrentIndex -= 1;
        }

        private void BtnImgNext_Click(object sender, EventArgs e)
        {
            ImgCurrentIndex -= 1;
            if (!ShowPic())
                ImgCurrentIndex += 1;
        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            xmlHelper.Write(Setting.ImgRegex, txtRegex.Text);
            xmlHelper.Write(Setting.BingUrl, txtBingUrl.Text);
            xmlHelper.Write(Setting.ImgType, txtImgType.Text);
            xmlHelper.Write(Setting.ServiceCron, txtServiceCron.Text);
            MessageBox.Show("保存成功");
        }
    }
}
